﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeWork.Library.DataStructure;

namespace BDHomePage.BLL
{
    public class LinkBasket
    {
        private ReadWriteCustomCollection<Link> _links;
        public ReadOnlyCustomCollection<Link> Links
        {
            get { return this._links.ReadOnlyClone(); }
        }

        public LinkBasket()
        {
            _links = new ReadWriteCustomCollection<Link>();
        }

        public void AddLinkToBasket(Link linkToAdd)
        {
            this._links.Add(linkToAdd);
            // Db Code to add link to basket
        }

        public void RemoveLinkFromBasket(Link linkToRemove)
        {
            this._links.Remove(linkToRemove);
            // DB Code to remove link from basket
        }

        public void AddLinksToGroup(LinkGroup linkGroup)
        {
            foreach (Link link in Links)
            {
                linkGroup.AddLinkToGroup(link);
            }
            linkGroup.UpdateLinkGroup();
        }

        public void AddLinksToCategory(LinkCategory linkCategory)
        {
            foreach (Link link in Links)
            {
                if(link.Selected)
                    linkCategory.AddLinkToCategory(link);
            }
        }

        public void CreateGroupWithLinks(string groupTitle, string groupDescription, int sortOrder)
        {
            LinkGroup newLinkGroup = new LinkGroup();
            CustomCollection<Link> selectedLinks = new ReadWriteCustomCollection<Link>();

            foreach (Link link in Links)
            {
                if (link.Selected)
                {
                    newLinkGroup.AddLinkToGroup(link);
                    RemoveLinkFromBasket(link);
                }
            }

            newLinkGroup.GroupTitle = groupTitle;
            newLinkGroup.GroupDescription = groupDescription;
            newLinkGroup.SortOrder = sortOrder;
            newLinkGroup.AddLinkGroup();
        }

        public void ClearBasket()
        {
            this._links.Clear();
            // DB Code to clear basket
        }
    }
}
